define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var salary_id = Config.salary_id;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'salary/salary_benchmark_data/index' + location.search,
                    add_url: 'salary/salary_benchmark_data/add?salary_id=' + salary_id,
                    del_url: 'salary/salary_benchmark_data/del',
                    multi_url: 'salary/salary_benchmark_data/multi',
                    import_url: 'salary/salary_benchmark_data/import?salary_id=' + salary_id,
                    first_trial_pass_through_url: 'salary/salary_benchmark_data/first_trial_pass_through',
                    first_trial_reject_url: 'salary/salary_benchmark_data/first_trial_reject',
                    end_trial_pass_through_url: 'salary/salary_benchmark_data/end_trial_pass_through',
                    end_trial_reject_url: 'salary/salary_benchmark_data/end_trial_reject',
                    table: 'salary_benchmark_data',
                }
            });

            function getUrlParam(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) {
                    return decodeURIComponent(r[2]);
                }
                return null;
            }
            var type = getUrlParam('type');
            var admin_check_auth = $("#admin_check_auth").val();
            var button = {};
            if(type == 1){
                if(admin_check_auth == 1){
                    button = [{
                        name: 'first_trial_pass_through',
                        text: '通过I',
                        confirm: '确定操作吗？',
                        icon: '',
                        classname: 'btn btn-xs btn-primary btn-ajax',
                        url: 'salary/salary_benchmark_data/first_trial_pass_through',
                        success: function (data, ret) {
                            $(".btn-refresh").trigger("click");
                            //如果需要阻止成功提示，则必须使用return false;
                            //return false;
                        },
                    },
                        {
                            name: 'first_trial_reject',
                            text: '驳回I',
                            icon: '',
                            classname: 'btn btn-xs btn-primary btn-dialog',
                            url: 'salary/salary_benchmark_data/first_trial_reject',
                        }];
                }else if(admin_check_auth == 2){
                    button = [
                        {
                            name: 'end_trial_pass_through',
                            text: '通过II',
                            confirm: '确定操作吗？',
                            icon: '',
                            classname: 'btn btn-xs btn-primary btn-ajax',
                            url: 'salary/salary_benchmark_data/end_trial_pass_through',
                            success: function (data, ret) {
                                $(".btn-refresh").trigger("click");
                                //如果需要阻止成功提示，则必须使用return false;
                                //return false;
                            },
                        },
                        {
                            name: 'end_trial_reject',
                            text: '驳回II',
                            icon: '',
                            classname: 'btn btn-xs btn-primary btn-dialog',
                            url: 'salary/salary_benchmark_data/end_trial_reject',
                        }];
                }

            }
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                queryParams: function (params) {
                    params['salary_id'] = salary_id;
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'id_card', title: __('Id_card'), operate: false},
                        {field: 'num', title: __('Num'), operate: false},
                        {field: 'select_time', title: __('Select_time'),datetimeFormat:'YYYY-MM-DD', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'),searchList: {"1": '待审核', "2": '初审通过/终审待审核','3':'初审已驳回','4':'终审已通过','5':'终审已驳回'},formatter: Table.api.formatter.status,custom:{ 1:'warning',2:'warning',3:'danger',4:'success',5:'danger'}},
                        {field: 'first_trial_admin_name', title: '初审人员', operate:false},
                        {field: 'first_trial_admin_signature', title: '初审电子签章', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'first_trial_time', title: '初审时间',datetimeFormat:'YYYY-MM-DD', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'first_trial_reject_reason', title: '初审驳回原因', operate:false},
                        {field: 'end_trial_admin_name', title: '终审人员', operate:false},
                        {field: 'end_trial_admin_signature', title: '终审电子签章', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'end_trial_time', title: '终审时间',datetimeFormat:'YYYY-MM-DD', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_trial_reject_reason', title: '终审驳回原因', operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'),
                            buttons: button,
                            table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            formatter: function (value, row, index) { //隐藏自定义的视频按钮
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                //权限判断
                                if(row.status != 1){ //通过Config.chapter 获取后台存的chapter
                                    $(table).data("operate-first_trial_pass_through", null);
                                    $(table).data("operate-first_trial_reject", null);
                                }
                                if(row.status != 2){ //通过Config.chapter 获取后台存的chapter
                                    $(table).data("operate-end_trial_pass_through", null);
                                    $(table).data("operate-end_trial_reject", null);
                                }
                                if(row.status == 4){
                                    $(table).data("operate-del", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
             $('.batch-examine').on('click', function() {
                var ids = Table.api.selectedids(table);
                var type = $(this).data('type');
                var reason = '';
                if(type == 'reject'){
                    reason = prompt("请输入原因", "");
                    if(!name){
                        Toastr.error('请输入原因');return

                    }
                }
                $.ajax({
                    url: 'salary/salary_benchmark_data/batch_examine',
                    type: 'POST',
                    data: {
                        ids: JSON.stringify(ids),
                        type:type,
                        reason:reason
                    },
                    success: function(data) {
                        if(data.code === 1){
                            Toastr.success(data.msg);
                            setTimeout(function() {
                                $(".btn-refresh").trigger("click");
                            }, 300);
                        }else{
                            Toastr.error(data.msg);
                        }
                    },
                    error: function(xhr, status, error) {
                        Toastr.error('请求失败'); return false;
                    }
                });
            });

            $('.batch-examine_two').on('click', function() {
                var ids = Table.api.selectedids(table);
                var type = $(this).data('type');
                var reason = '';
                if(type == 'reject'){
                    reason = prompt("请输入原因", "");
                    if(!name){
                        Toastr.error('请输入原因');return

                    }
                }
                $.ajax({
                    url: 'salary/salary_benchmark_data/batch_examine_two',
                    type: 'POST',
                    data: {
                        ids: JSON.stringify(ids),
                        type:type,
                        reason:reason
                    },
                    success: function(data) {
                        if(data.code === 1){
                            Toastr.success(data.msg);
                            setTimeout(function() {
                                $(".btn-refresh").trigger("click");
                            }, 300);
                        }else{
                            Toastr.error(data.msg);
                        }
                    },
                    error: function(xhr, status, error) {
                        Toastr.error('请求失败'); return false;
                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        first_trial_pass_through: function () {
            Controller.api.bindevent();
        },
        first_trial_reject: function () {
            Controller.api.bindevent();
        },
        end_trial_pass_through: function () {
            Controller.api.bindevent();
        },
        end_trial_reject: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                Fast.api.teacher_Select('.select', {
                    multiple: false,
                    type:true,
                    callback: function (data) {
                        setTeacher(data);
                    }
                });


                var setTeacher = function (data) {
                    if(data && data[0]){
                        $('#nickname').val(data[0].nickname);
                        $('#admin_id').val(data[0].id);
                    }
                };
            }
        }
    };
    return Controller;
});
